<?php
namespace app\dlc\controller;
use app\common\model\Site;

use think\Db;

class FormController extends CommonController
{

    public function _initialize(){
        parent::_initialize();

    }
    public function index()
    {

        $this->headBotton('','add','添加','/'.MODULE_NAME.'/'.$this->className.'/_edit');
        $this->headSearch('','name|1','表名称','1');
        $this->listBotton('','edit','编辑','/'.MODULE_NAME.'/'.$this->className.'/_edit_ajax',2);
        $this->listBotton('','history','字段列表','/'.MODULE_NAME.'/formItem/index');
        $this->listBotton('','add','生成字段','/'.MODULE_NAME.'/'.$this->className.'/key',2,'','','',['status','=',1],['onClick' => 'openNewWindows']);
        $this->listBotton('','edit','添加字段','/'.MODULE_NAME.'/'.$this->className.'/add_key',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','delete','删除','/'.MODULE_NAME.'/'.$this->className.'/_del');
        $this->loading();
        $map = $this->map;
        $data = model($this->className)->where($map)->order('id desc')->page(self::$page)->select();
        foreach ($data as $key => $value) {
            $keys = Db::name('form_item')->where(['form_id'=>$value['id']])->order('sort asc')->column('key');
            $data[$key]['keys'] = implode(',',$keys);
        }
        $count = Db::name($this->className)->where($map)->count();
        $this->getPage($count, self::$psize, 'App-loader', $this->title, 'App-search');
        $this->assign('data',$data);
        $list = ['id'=>'ID','index_name'=>'识别名（唯一）','name'=>'表名称','keys'=>'显示的字段','ctime'=>'创建时间','sys'=>'操作'];
        $this->assign('list',$list);
        echo $this->fetch('common/index');
    }

    public function _edit($updata = false, $add = false, $noRepeat = false, $funName = false)
    {

//        $this->formAddArray = $data;
        parent::_edit($updata, $add, $noRepeat, $funName); // TODO: Change the autogenerated stub
    }

    public function key(){
        $post = input('post.');
        $id = input('id');
        $table = Db::name('form')->where(['id'=>$id])->value('name');
        $data = \db()->getTableInfo('dlc_'.$table,'fields');
        $add['key']['data'] = $data;
        $this->formAddArray = $add;
        $this->form = 'form_key';
        if (count($post)>1){
            $key_name = self::get_db_column_comment($table);
            $i=0;
            foreach ($post['key'] as $key=>$value){
                $i++;
                $add = [];
                $k = $data[$value];
                $add['form_id'] = $id;
                $add['form_name'] = $table;
                $add['key'] = $k;
                $add['title'] = $key_name[$k];
                $add['sort'] = $i;
                $add['ctime'] = time();
                $res = Db::name('form_item')->where(['key'=>$add['key'],'form_id'=>$add['form_id']])->find()?:'';
                if (empty($res)){
                    Db::name('form_item')->insert($add);
                }
            }
            Db::name('form')->where(['id'=>$id])->update(['status'=>2]);
            admin_return(1,'成功');
        }
        $this->_edit_ajax();
    }

    public function add_key(){
        $this->form = 'form_item_add';

        $post = input('post.');
        $id = input('id');
        if (count($post)>1){
            $form = Db::name('form')->find($post['id']);
            $formData = \db()->getTableInfo('dlc_'.$form['name'],'fields');
            if (!in_array($post['key'],$formData)) {admin_return(0,'该字段不存在');}
           $form = Db::name('form')->find($id);
           if ($form){
               $post['form_id'] = $id;
               $post['form_name'] = $form['name'];
               $post['ctime'] = time();
               unset($post['id']);
               $res = Db::name('form_item')->insert($post);
               if ($res) admin_return(1,'成功');
               admin_return(1,'不能重复插入');

           }else{
               admin_return(0,'错误');
           }

        }

        $this->_edit_ajax();
    }
    public function _del($tabelName = false, $tableKey = 'id', $child = false, $delChild = false)
    {
        $id  = input('id');
        $delChild['tableName'] = 'form_item';
        $delChild['key'] = 'form_id';
//        dump($delChild);die;
        parent::_del($tabelName, $tableKey, $child, $delChild); // TODO: Change the autogenerated stub

    }

    public function _edit_ajax($updata = false, $add = false, $noRepeat = false, $funName = false)
    {
        parent::_edit_ajax($updata, $add, $noRepeat, $funName); // TODO: Change the autogenerated stub
    }


}